import commonConstants from "@/common/commonConstants";
import commonMethod from "@/common/commonMethod";
import { Cartesian2, Cartesian3, ClassificationType, Color, HeightReference, ImageMaterialProperty, LabelStyle, Plane } from "cesium";

/**
 * 此文件夹下都是合成的 Entity 
 * 此为军标标绘
 */
export default class JBEntity{
    curViewer;
    id;
    showForm;
    params;
    entities;
    annotationEntity;
    monitorKey;
    entityType;
    isShow;
    jbName;
    constructor(viewer){
        const _this = this;
        _this.curViewer = viewer;
        _this.id = commonMethod.creatUUID();
        _this.showForm = commonConstants.jbShowForm.relativeScreen;
        _this.entities = [];
        _this.isShow = true;
        _this.jbName = "";
    }


    setParams(p){
        const _this = this;
        _this.params = p;
        if (_this.params.showForm){
            _this.showForm = _this.params.showForm;
        }
        if (_this.params.markerId){
            _this.id = _this.params.markerId;
        }
        if (_this.params.markerName){
            _this.jbName = _this.params.markerName;
        }
    }

    setMonitorKey(monitorKey){
        const _this = this;
        _this.monitorKey = monitorKey;
    }

    getEntityId(){
        return this.id;
    }

    setEntityType(entityType){
        const _this = this;
        _this.entityType = entityType;
    }

    /**
     * 创建军标标绘
     * @param {Cartesian3} position 
     */
    createJb(position){
        let _this = this;
        if (_this.showForm === commonConstants.jbShowForm.groundLayer){
            //贴地军标,创建两个标绘，一个图，一个注记
            _this.drawPic(position);
            let annotationPosition = _this.costAnnotationPos(position);
            _this.drawAnnotion(annotationPosition);
        }else{
            //相对屏幕，一个boll开始
            const markerCode = _this.params.markerCode;
            let color = Color.RED.withAlpha(.8);
            let camp = _this.params.camp;
            if (camp) {
                if (camp === commonConstants.troopCamp.our_enemy) {
                    color = Color.BLUE.withAlpha(.8);
                } else if (camp === commonConstants.troopCamp.our_friend) {
                    color = Color.PINK.withAlpha(.8)
                }
            }
            let entity = _this.curViewer.entities.add({
                pid:_this.id,
                position:position,
                billboard:{
                    image:markerCode.getMarkerPath()
                },
                label:{
                    text:_this.params.markerName,
                    font: '18px "微软雅黑", Arial, Helvetica, sans-serif, Helvetica',
                    fillColor: color,
                    outlineColor: color,
                    outlineWidth: 1,
                    style: LabelStyle.FILL_AND_OUTLINE,
                    pixelOffset:new Cartesian2(0,60),
                    heightReference:HeightReference.CLAMP_TO_GROUND,
                    disableDepthTestDistance: Number.POSITIVE_INFINITY
                }
            });
            entity.monitorKey = _this.monitorKey;
            entity.entityType = _this.entityType;
            _this.entities.push(entity);
        }
    }

    /**
     * 将图片画入画布地球中
     * @param {Cartesian3} cartesian 
     */
    drawPic(cartesian){
        let _this = this;
        const markerCode = _this.params.markerCode;
        let camp = _this.params.camp;
        let pic = markerCode.getMarkerPath();
        
        // let splices = pic.split("/");
        // let last = splices[splices.length -1];
        // if (camp){
        //     if (camp === commonConstants.troopCamp.our_side){
        //         splices[splices.length-1] = last.replace(".svg", "-red.png");
        //     }else if(camp === commonConstants.troopCamp.our_enemy){
        //         splices[splices.length-1] = last.replace(".svg", "-blue.png");
        //     }else if(camp === commonConstants.troopCamp.our_friend){
        //         splices[splices.length-1] = last.replace(".svg", "-pink.png");
        //     }
        // } else{
        //     splices[splices.length-1] = last.replace(".svg", "-red.png");
        // }

        // let c = splices.join("/");
        let entity = _this.curViewer.entities.add({
            pid:_this.id,
            position:cartesian,
            plane:{
                plane:new Plane(Cartesian3.UNIT_Z,0.0),
                dimensions:new Cartesian2(420,420),
                material:new ImageMaterialProperty({
                    image: pic,
                    transparent:true
                }),
                fill:true,
                show:true,
                classificationType:ClassificationType.BOTH
            }
        });
        entity.monitorKey = _this.monitorKey;
        entity.entityType = _this.entityType;
        _this.entities.push(entity);
    }

    /**
     * 注释放入军标下面
     * @param {Cartesian3} position 
     * @returns 
     */
    costAnnotationPos(position){
        return new Cartesian3(position.x-24.988369965925813,position.y+110.1290402924642,position.z-231.24011346837506);
    }

     /**
     * 对标绘描绘出注记
     * @param {Cartesian3} cartesian 
     */
     drawAnnotion(cartesian){
        let _this = this;
        let text = _this.params.markerName;
        _this.annotationEntity = _this.curViewer.entities.add({
            pid:_this.id,
            position: cartesian,
            plane:{
                plane:new Plane(Cartesian3.UNIT_Z,0.0),
                dimensions:_this.getSize(text),
                material:new ImageMaterialProperty({
                    image: _this.drawText(text),
                    transparent:true,
                    color:Color.WHITE
                }),
                fill:true,
                classificationType:ClassificationType.BOTH
            }
        });
        _this.annotationEntity.monitorKey = _this.monitorKey;
        _this.annotationEntity.entityType = _this.entityType;
        _this.entities.push(_this.annotationEntity);
    }

    getSize(text){
        let _this = this;
        // let fontSize = _this.params.fontSize? _this.params.fontSize:16;
        let scale = _this.params.scale?_this.params.scale:1;
        // let width = (text + "").length * fontSize;
        // let height = fontSize;

        return new Cartesian2(200*scale,60*scale);
    }

    drawText(text){
        let _this = this;
        let fontSize = _this.params.fontSize? _this.params.fontSize:16;
        let textColor = _this.params.color?_this.params.color:"red";
        let camp = _this.params.camp;
        if (camp){
            textColor = camp;
        }
        let scale = _this.params.scale?_this.params.scale:1;
        let c = document.createElement("canvas");
        c.width = (text + " ").length * fontSize;
        c.height = fontSize + 5;
        let ctx = c.getContext("2d");

        ctx.fillStyle = textColor;
        ctx.font = fontSize+"px 宋体";
        ctx.textBaseline = "hangind";
        ctx.fillText(text,10,15 * scale);
        return c;
    }
    updateEntity(){
        let _this = this;
        _this.annotationEntity.plane.material.image = _this.drawText(_this.jbName);
        _this.annotationEntity.plane.dimensions = _this.getSize(_this.jbName);
    }

    setAnnotationText(text){
        let _this = this;
        _this.jbName = text;
        if (_this.showForm === commonConstants.jbShowForm.relativeScreen){
            _this.entities[0].label.text = text;
        }else{
            _this.updateEntity();
        }
    }
     /**
     * 移动jb
     * @param cartesian {Cartesian3}
     */
     movePositon(cartesian) {
        let _this = this;
        _this.entities[0].position = cartesian;
        if (_this.showForm === commonConstants.jbShowForm.groundLayer) {
            _this.annotationEntity.position = _this.costAnnotationPos(cartesian);
        }
    }

    destroy () {
        let _this = this;
        for (let i = 0; i < _this.entities.length; i++) {
            _this.curViewer.entities.remove(_this.entities[i]);
        }
    }
};